const Player = require("./player");

let playerManager = (function () {
    let that = {};
    let playerList = [];

    // 创建玩家
    that.creatPlayer = function (playerInfo, ws, GameCtr) {
        let player = new Player(playerInfo, ws, GameCtr);
        playerList.push(player);
    };

    // 删除玩家
    that.removePlayer = function (player) {
        let index = playerList.indexOf(player);
        playerList.splice(index, 1);
    };

    // 全服广播
    that.broadcastToAllPlayer = function (data) {
        for (let i = 0; i < playerList.length; i++) {
            if (!playerList[i].roomID) {
                playerList[i].sendMsg(data);
            }
        }
    };

    return that;
})();

module.exports = playerManager;
